﻿

namespace KingerRobot.MexicoSailunMDC.SpiService.Managers;
internal class LocSkuManager
{
    protected readonly Db m_Db;


    public LocSkuManager(Db db = null)
    {
        this.m_Db = db ?? Db.Default;
    }

    public int SaveChanges()
    {
        return this.m_Db.SaveChanges();
    }


    #region 公共

    public POProdProduct ClearLocHasTask(long locId)
    {
        var locSkus = this.m_Db.WcsLocSkus.Where(f => f.LocId == locId).ToList();

        if (locSkus is not null && locSkus.Any())
        {
            locSkus.ForEach(item => { this.m_Db.WcsLocSkus.Remove(item); });
        }

        var productId = locSkus.OrderByDescending(f => f.CreateTime).FirstOrDefault()?.SkuId ?? 0;

        var product = this.m_Db.ProdProducts.Where(f => f.Id == productId).FirstOrDefault();

        var loc = this.m_Db.RcsLocations.Where(f => f.Id == locId).FirstOrDefault();

        if (loc is not null)
        {
            loc.LocHasSku = 0;
            loc.LocHasSkuTime = null;
            loc.TaskOccupy = 0;
            loc.TaskOccupyTime = null;
            loc.TaskId = null;
            loc.MaterialId = 0;
        }

        return product;
    }

    public void UpdateTaskRemarkById(long? taskId, string remark)
    {
        var task = this.m_Db.WcsTasks.Where(f => f.Id == taskId).FirstOrDefault();

        if (task is not null)
        {
            task.RemarkMemo = remark;
        }
    }

    public void SetLocHasTask(long locId, long skuId, long taskId = 0, long? materialId = 0, bool isBindTask = true)
    {
        var loc = this.m_Db.RcsLocations.Where(f => f.Id == locId).FirstOrDefault();

        if (loc is null) return;

        if (loc is not null)
        {
            loc.LocHasSku = 1;
            loc.LocHasSkuTime = DateTime.Now;
            loc.MaterialId = materialId;

            if (taskId > 0 && isBindTask)
            {
                loc.TaskOccupy = 1;
                loc.TaskOccupyTime = DateTime.Now;
                loc.TaskId = taskId;
            }
        }

        var bean = new POWcsLocSku()
        {
            Id = XID.Nextval,
            LocId = locId,
            SkuId = skuId,
        };

        if (taskId > 0)
        {
            bean.TaskId = taskId;
        }

        this.m_Db.WcsLocSkus.Add(bean);

    }

    #endregion

    #region 入库取胎位

    public void ClearInputLocHasTask(long locId)
    {
        var product = this.ClearLocHasTask(locId);

        if (product is null)
        {
            product = new();
        }

        //XLogHelper.LogToDb(this.m_Db, $"入库绑定-{locId},清除绑定关系，清除条码：{product.ProductCode}", WcsLogMsgLevel.Debug, "清除取胎位绑定", product);
    }

    //public void SetInputLocHasTask(long locId, long productId, long materialId, long taskId, string skuCode, long inWareHouseId)
    //{
    //    WcsInWarehouseHelpers.UpdateArriveTime(this.m_Db, inWareHouseId);

    //    this.SetLocHasTask(locId, productId, taskId, materialId);


    //    XLogHelper.LogToDb(this.m_Db, $"入库绑定-{locId},绑定ProductId{productId},绑定任务Id：{taskId}", WcsLogMsgLevel.Debug, "取胎位绑定", skuCode);
    //}

    #endregion

    #region 出库创建

    public void SetLocTask(long? locId, long taskId)
    {
        var location = this.m_Db.RcsLocations.Where(f => f.Id == locId).FirstOrDefault();

        if (location is not null)
        {
            location.TaskId = taskId;
            location.TaskOccupy = 1;
            location.TaskOccupyTime = DateTime.Now;
        }
    }

    #endregion

    #region 入库/出库完成

    public void UpdateLocationAndWcsLocWithSkuId(long locId, long productId, long materialId, long taskId, string skuCode, int type, bool isBindTask = true)
    {

        var locSku = this.m_Db.WcsLocSkus.Where(f => f.SkuId == productId).FirstOrDefault();

        if (locSku is not null)
        {
            this.ClearLocHasTask(locSku?.LocId ?? 0);

           // XLogHelper.LogToDb(this.m_Db, $"入/出库-{type}-完成-清除产品原有绑定关系，原有信息：{locSku.ToJson()}", WcsLogMsgLevel.Debug, "清除原绑定关系", skuCode);
        }

        this.SetLocHasTask(locId, productId, taskId, materialId, isBindTask);



        //XLogHelper.LogToDb(this.m_Db, $"入/出库-{type}-完成-{locId},绑定ProductId{productId},绑定任务Id：{taskId}", WcsLogMsgLevel.Debug, "库位设置绑定", skuCode);
    }

    #endregion

}
